package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.VoucherAdjustOrder;
import com.turing.post.organization.util.VoucherAdjustOrderBusiness;
import com.turing.post.organization.util.VoucherAdjustOrderDao;
import com.turing.post.organization.util.VoucherAdjustOrderDetail;
import com.turing.post.organization.util.VoucherAdjustOrderDetailBusiness;
import com.turing.post.organization.util.VoucherAdjustOrderDetailDao;
import com.turing.post.organization.util.VoucherStock;
import com.turing.post.organization.util.VoucherStockBusiness;
import com.turing.post.organization.util.VoucherStockDao;
import com.turing.post.organization.util.VoucherStockDetail;
import com.turing.post.organization.util.VoucherStockDetailBusiness;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherAdjustOrderBusinessImpl")
@Remote
@Local
public class VoucherAdjustOrderBusinessImpl implements
		VoucherAdjustOrderBusiness {

	@EJB(mappedName = "organization.impl.VoucherAdjustOrderDaoImpl")
	private VoucherAdjustOrderDao voucherAdjustOrderDaoImpl;// 凭证调整单

	@EJB(mappedName = "organization.impl.VoucherStockDaoImpl")
	private VoucherStockDao voucherStockDaoImpl;// 凭证库存

	@EJB(mappedName = "organization.impl.VoucherStockBusinessImpl")
	private VoucherStockBusiness voucherStockBusinessDaoImpl;// 凭证库存业务

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;// 日志

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;// 主键

	@EJB(mappedName = "organization.impl.VoucherAdjustOrderDetailBusinessImpl")
	private VoucherAdjustOrderDetailBusiness voucherAdjustOrderDetailBusinessImpl;// 凭证调整单明细表业务

	@EJB(mappedName = "organization.impl.VoucherAdjustOrderDetailDaoImpl")
	private VoucherAdjustOrderDetailDao voucherAdjustOrderDetailDaoImpl;// 凭证调整单明细表
	
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;// 发卡商
	
	@EJB(mappedName = "organization.impl.VoucherStockDetailBusinessImpl")
	private VoucherStockDetailBusiness voucherStockDetailBusinessDaoImpl;// 凭证库存详细业务

	public BigInteger insertVoucherAdjustOrder(PCPSWebInput webInput,
			VoucherAdjustOrder voucherAdjustOrder,
			List<VoucherAdjustOrderDetail> datil)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 操作员
		if (webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"操作员为空!");
		}
		// 交易类型
		String tranType=voucherAdjustOrder.getTranType();
		if (PCPSUtil.isEmpty(tranType)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"交易类型为空!");
		}
		// 调整单ID
		BigInteger voucherAdjustOrderId = pcpsCommonDaoImpl
				.getUniqueIntegerKey();
		if (voucherAdjustOrderId == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"创建调整单ID为空!");
		}
		voucherAdjustOrder.setIssueCompanyNo(webInput.getOperBranchNo()
				.substring(0, 6));
		voucherAdjustOrder.setStatus(VoucherAdjustOrder.Status_0);
		voucherAdjustOrder.setOperId(webInput.getOperId());
		voucherAdjustOrder.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		voucherAdjustOrder.setVoucherAdjustOrderId(voucherAdjustOrderId);
		voucherAdjustOrder.setBranchNo(webInput.getOperBranchNo());
		// 插入凭证调整单
		voucherAdjustOrderDaoImpl.insertVoucherAdjustOrder(voucherAdjustOrder);
		// 得到入库明细
		int k = 1;
		HashMap<String,String> startseqmap=new HashMap<String,String> ();
		HashMap<String,String> endseqmap=new HashMap<String,String> ();
		if(datil==null || datil.size()==0){
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"", "凭证号段为空!");
		}
		
		for (int i = 0; i < datil.size(); i++) {
			VoucherAdjustOrderDetail voucherdetail = (VoucherAdjustOrderDetail) datil
					.get(i);
			// 起始号
			String StartVoucherNo = voucherdetail.getStartVoucherNo().trim();
			// 终止号
			String EndVoucherNo = voucherdetail.getEndVoucherNo().trim();
			if(PCPSUtil.isEmpty(StartVoucherNo)){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第"+k+"行起始号为空!");
			}
			if(PCPSUtil.isEmpty(EndVoucherNo)){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第"+k+"行终止号为空!");
			}
			BigInteger intototal = new BigInteger(EndVoucherNo)
					.subtract(new BigInteger(StartVoucherNo));
			intototal = intototal.add(BigInteger.ONE);
			
			if(startseqmap.get(StartVoucherNo)!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			
			if(endseqmap.get(EndVoucherNo.trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行终止号已经重复录入!");
			}
			
			if(endseqmap.get(StartVoucherNo.trim())!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			if(startseqmap.get(EndVoucherNo)!=null){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行终止号已经重复录入!");
			}
			if(isVoucherNo(StartVoucherNo.trim(),startseqmap).endsWith("error")){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
			if(isVoucherNo(EndVoucherNo.trim(),startseqmap).endsWith("error")){
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"","第"+k+"行起始号已经重复录入!");
			}
           // 得到入库总数
			BigInteger stocktotal =BigInteger.ZERO;
			if(tranType.equals(VoucherAdjustOrder.TranType_01)|| tranType.equals(VoucherAdjustOrder.TranType_02)){
				stocktotal = voucherStockDaoImpl
						.getVoucherStockListTotal(webInput, voucherdetail
								.getVoucherId(), StartVoucherNo, EndVoucherNo,
								VoucherStock.Status_0);
				if (intototal.compareTo(stocktotal)!=0) {
					throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
							"", "第"+k+"行号段有误!或凭证种类有误!或凭证号状态已不是正常状态!或不在本机构下!");
				}
			}else{
				stocktotal = voucherStockDaoImpl
				.getVoucherStockListTotal(webInput, voucherdetail
						.getVoucherId(), StartVoucherNo, EndVoucherNo,
						VoucherStock.Status_9);
				if (intototal.compareTo(stocktotal)!=0) {
					throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
							"", "第"+k+"行号段有误!或凭证种类有误!或凭证号状态已不是丢失、损坏等!或不在本机构下!");
				}
			}
			startseqmap.put(StartVoucherNo.trim(),EndVoucherNo.trim());
			endseqmap.put(EndVoucherNo.trim(), StartVoucherNo.trim());
			// 凭证调整单明细
			VoucherAdjustOrderDetail voucherAdjustOrderDetail = new VoucherAdjustOrderDetail();
			voucherAdjustOrderDetail.setCount(stocktotal.intValue());
			voucherAdjustOrderDetail.setEndVoucherNo(EndVoucherNo);
			voucherAdjustOrderDetail.setStartVoucherNo(StartVoucherNo);
			voucherAdjustOrderDetail
					.setVoucherAdjustOrderId(voucherAdjustOrderId);
			voucherAdjustOrderDetail
					.setSeqNo(new BigInteger(String.valueOf(k)));
			voucherAdjustOrderDetail.setVoucherId(voucherdetail.getVoucherId());
			// 插入凭证调整单明细
			voucherAdjustOrderDetailBusinessImpl
					.insertVoucherAdjustOrderDetail(webInput,
							voucherAdjustOrderDetail);
			k++;
		}
		// 修改数据库操作日志
		insertRecordingOperationLog(webInput, "凭证调整单新增", voucherAdjustOrder,
				SysOperLog.operType_Edit, "VoucherAdjustOrderId:"
						+ voucherAdjustOrder.getVoucherAdjustOrderId());
		return voucherAdjustOrderId;
	}

	/**
	 * 修改凭证调整单状态-审核
	 * 
	 * @param VoucherAdjustOrder
	 *            凭证调整单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherAdjustOrderByAuditing(PCPSWebInput webInput,
			VoucherAdjustOrder voucherAdjustOrder)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 调整单ID
		if (voucherAdjustOrder.getVoucherAdjustOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类ID为空!");
		}
		VoucherAdjustOrder voucherAdjustOrder1 = voucherAdjustOrderDaoImpl
				.getVoucherAdjustOrderById(voucherAdjustOrder
						.getVoucherAdjustOrderId());
		if (voucherAdjustOrder1 == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单不存在!");
		}
		if (!voucherAdjustOrder1.getStatus()
				.equals(VoucherAdjustOrder.Status_0)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单状态已不是待审核状态!");
		}
		//System.out.println(voucherAdjustOrder1.getOperId()+"---"+webInput.getOperId());
		if (voucherAdjustOrder1.getOperId().equals(webInput.getOperId())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"审核人和申请人不能是同一人!请合理分配角色!");
		}
		voucherAdjustOrder1.setStatus(VoucherAdjustOrder.Status_1);
		voucherAdjustOrder1.setAuthOperId(webInput.getOperId());
		voucherAdjustOrder1.setAuthTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		voucherAdjustOrder1.setRemark(voucherAdjustOrder.getRemark());
		// 凭证调整单
		voucherAdjustOrderDaoImpl
				.updateVoucherAdjustOrderByStatus(voucherAdjustOrder1);
		// 修改库存
		List<VoucherAdjustOrderDetail> datillist = voucherAdjustOrderDetailDaoImpl
				.getVoucherAdjustOrderDetailListNoPage(webInput,
						voucherAdjustOrder1.getVoucherAdjustOrderId());
		if (datillist == null || datillist.size() == 0) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单明细为空!");
		}
		VoucherStockDetail voucherStockDetail = null;
		VoucherAdjustOrderDetail detail =null;
		// 发卡商
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(webInput.getOperBranchNo()
						.substring(0, 6));
		for (int i = 0; i < datillist.size(); i++) {
			detail = (VoucherAdjustOrderDetail) datillist
					.get(i);
			// 起始号
			String StartVoucherNo = detail.getStartVoucherNo().trim();
			// 终止号
			String EndVoucherNo = detail.getEndVoucherNo().trim();
			BigInteger intototal = new BigInteger(EndVoucherNo)
					.subtract(new BigInteger(StartVoucherNo));
			intototal = intototal.add(new BigInteger("1"));
			// 得到入库总数
			/*BigInteger stocktotal = voucherStockDaoImpl
					.getVoucherStockListTotal(webInput, detail.getVoucherId(),
							StartVoucherNo, EndVoucherNo, VoucherStock.Status_0);
			if (stocktotal.compareTo(intototal)!=0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证号号段有误!或凭证种类有误!或凭证号状态已不是正常状态");
			}*/
			// 修改库存
			String status=VoucherStock.Status_0;
			String status_1=VoucherStock.Status_0;
			if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_01)){
				status=VoucherStock.Status_0;
				status_1=VoucherStock.Status_9;
			}
			else if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_02)){
				status=VoucherStock.Status_0;
				status_1=VoucherStock.Status_9;
			}
			else if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_03)){
				status=VoucherStock.Status_9;
				status_1=VoucherStock.Status_0;
			}
			//System.out.println("=="+voucherAdjustOrder1.getTranType());
			List<VoucherStock> stocklist = voucherStockDaoImpl
					.getVoucherStockListNoPage(webInput, StartVoucherNo,
							EndVoucherNo,status, detail
									.getVoucherId());

			if (stocklist == null || stocklist.size() <= 0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证号号段有误库存不存在!");
			}
			for (int ii = 0; ii < stocklist.size(); ii++) {
				VoucherStock stock = stocklist.get(ii);
				// 得到入库单ID
				VoucherStock Isvoucherstock = voucherStockDaoImpl
						.getVoucherStock(stock);
				if (Isvoucherstock == null) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "凭证号已经不存在!");
				}				
			    Isvoucherstock.setStatus(status_1);
				voucherStockBusinessDaoImpl.updateVoucherStockByStatus(
						webInput, Isvoucherstock);
			}
			// 凭证出入库明细
			voucherStockDetail = new VoucherStockDetail();
			voucherStockDetail.setIoOrderId(voucherAdjustOrder1.getVoucherAdjustOrderId());
			voucherStockDetail.setBranchNo(voucherAdjustOrder1.getBranchNo());
			voucherStockDetail.setTranDate(ic.getTranDate());
			voucherStockDetail.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			voucherStockDetail.setIssueCompanyNo(voucherAdjustOrder1.getIssueCompanyNo());
			voucherStockDetail.setVoucherId(detail.getVoucherId());
			voucherStockDetail.setStartVoucherNo(detail.getStartVoucherNo());
			voucherStockDetail.setEndVoucherNo(detail.getEndVoucherNo());
			voucherStockDetail.setCount(detail.getCount());
			if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_01))
			   voucherStockDetail.setTranType(VoucherStockDetail.Status_9);
			else if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_02))
				   voucherStockDetail.setTranType(VoucherStockDetail.Status_9);
			else if(voucherAdjustOrder1.getTranType().equals(VoucherAdjustOrder.TranType_03))
				   voucherStockDetail.setTranType(VoucherStockDetail.Status_3);
			voucherStockDetail.setOperId(webInput.getOperId());//
			voucherStockDetailBusinessDaoImpl.insertVoucherStockDetail(webInput, voucherStockDetail);            
			
		}
		// 修改数据库操作日志
		insertRecordingOperationLog(webInput, "凭证调整单修改", voucherAdjustOrder1,
				SysOperLog.operType_Edit, "VoucherAdjustOrderId:"
						+ voucherAdjustOrder.getVoucherAdjustOrderId());
	}

	/**
	 * 修改凭证调整单状态-取消申请
	 * 
	 * @param VoucherAdjustOrder
	 *            凭证调整单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherAdjustOrderByCancelling(PCPSWebInput webInput,
			VoucherAdjustOrder voucherAdjustOrder)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 调整单ID
		if (voucherAdjustOrder.getVoucherAdjustOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"调整单ID为空!");
		}
		VoucherAdjustOrder voucherAdjustOrder1 = voucherAdjustOrderDaoImpl
				.getVoucherAdjustOrderById(voucherAdjustOrder
						.getVoucherAdjustOrderId());
		if (voucherAdjustOrder1 == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单不存在!");
		}
		if (!voucherAdjustOrder1.getStatus()
				.equals(VoucherAdjustOrder.Status_1)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单状态已不是有效状态!");
		}
		if (!voucherAdjustOrder1.getOperId().equals(webInput.getOperId())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"只有创建人才可发起撤销申请!请合理分配角色!");
		}
		voucherAdjustOrder1.setStatus(VoucherAdjustOrder.Status_0);
		voucherAdjustOrder1.setAuthOperId(webInput.getOperId());
		voucherAdjustOrder1.setAuthTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		voucherAdjustOrder1.setRemark(voucherAdjustOrder.getRemark());
		if (voucherAdjustOrder1.getTranType().equals(
				VoucherAdjustOrder.TranType_01)) {// 丢失取消
			voucherAdjustOrder1.setTranType(VoucherAdjustOrder.TranType_11);
		} else if (voucherAdjustOrder1.getTranType().equals(
				VoucherAdjustOrder.TranType_02)) {// 报损取消
			voucherAdjustOrder1.setTranType(VoucherAdjustOrder.TranType_12);
		} else if (voucherAdjustOrder1.getTranType().equals(
				VoucherAdjustOrder.TranType_03)) {// 盘盈取消
			voucherAdjustOrder1.setTranType(VoucherAdjustOrder.TranType_13);
		} else {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单状态已不是有效状态!");
		}
		// 凭证调整单
		voucherAdjustOrderDaoImpl
				.updateVoucherAdjustOrderByStatus(voucherAdjustOrder1);
		// 修改库存基础信息验证
		List<VoucherAdjustOrderDetail> datillist = voucherAdjustOrderDetailDaoImpl
				.getVoucherAdjustOrderDetailListNoPage(webInput,
						voucherAdjustOrder.getVoucherAdjustOrderId());
		if (datillist == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单明细为空!");
		}
		for (int i = 0; i < datillist.size(); i++) {
			VoucherAdjustOrderDetail detail = (VoucherAdjustOrderDetail) datillist
					.get(i);
			// 起始号
			String StartVoucherNo = detail.getStartVoucherNo();
			// 终止号
			String EndVoucherNo = detail.getEndVoucherNo();
			BigInteger intototal = new BigInteger(EndVoucherNo)
					.subtract(new BigInteger(StartVoucherNo));
			intototal = intototal.add(new BigInteger("1"));
			// 得到入库总数
			BigInteger stocktotal = voucherStockDaoImpl
					.getVoucherStockListTotal(webInput, detail.getVoucherId(),
							StartVoucherNo, EndVoucherNo, VoucherStock.Status_9);
			if (stocktotal.compareTo(intototal)!=0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证号号段有误!或凭证种类有误!或凭证号状态已不是正常状态");
			}			
			         
		}
		
		// 修改数据库操作日志
		insertRecordingOperationLog(webInput, "凭证调整单修改", voucherAdjustOrder1,
				SysOperLog.operType_Edit, "VoucherAdjustOrderId:"
						+ voucherAdjustOrder.getVoucherAdjustOrderId());
	}

	/**
	 * 修改凭证调整单状态-确认
	 * 
	 * @param VoucherAdjustOrder
	 *            凭证调整单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherAdjustOrderByConfirm(PCPSWebInput webInput,
			VoucherAdjustOrder voucherAdjustOrder)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 调整单ID
		if (voucherAdjustOrder.getVoucherAdjustOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"调整单ID为空!");
		}
		VoucherAdjustOrder voucherAdjustOrder1 = voucherAdjustOrderDaoImpl
				.getVoucherAdjustOrderById(voucherAdjustOrder
						.getVoucherAdjustOrderId());
		if (voucherAdjustOrder1 == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单不存在!");
		}
		if (!voucherAdjustOrder1.getStatus()
				.equals(VoucherAdjustOrder.Status_0)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单状态已不是有效状态!");
		}
		if (voucherAdjustOrder1.getOperId().equals(webInput.getOperId())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"审核人和申请人不能是同一人!请合理分配角色!");
		}
		// 凭证调整单//调整单ID
		
		// 修改
		voucherAdjustOrder1.setStatus(VoucherAdjustOrder.Status_1);
		voucherAdjustOrder1.setAuthOperId(webInput.getOperId());
		voucherAdjustOrder1.setAuthTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		voucherAdjustOrder1.setRemark(voucherAdjustOrder.getRemark());
		// voucherAdjustOrder1.setCancelVoucherAdjustOrderId(voucherAdjustOrderId);
		voucherAdjustOrderDaoImpl
				.updateVoucherAdjustOrderByStatus(voucherAdjustOrder1);
		// 修改库存基础信息验证
		List<VoucherAdjustOrderDetail> datillist = voucherAdjustOrderDetailDaoImpl
				.getVoucherAdjustOrderDetailListNoPage(webInput,
						voucherAdjustOrder.getVoucherAdjustOrderId());
		if (datillist == null || datillist.size() == 0) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单明细为空!");
		}
		VoucherAdjustOrderDetail detail =null;

		VoucherStockDetail voucherStockDetail=null;
		// 发卡商
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(webInput.getOperBranchNo()
						.substring(0, 6));
		for (int i = 0; i < datillist.size(); i++) {
			detail = (VoucherAdjustOrderDetail) datillist
					.get(i);
			// 起始号
			String StartVoucherNo = detail.getStartVoucherNo();
			// 终止号
			String EndVoucherNo = detail.getEndVoucherNo();
			BigInteger intototal = new BigInteger(EndVoucherNo)
					.subtract(new BigInteger(StartVoucherNo));
			intototal = intototal.add(new BigInteger("1"));
			// 得到入库总数
			BigInteger stocktotal = voucherStockDaoImpl
					.getVoucherStockListTotal(webInput, detail.getVoucherId(),
							StartVoucherNo, EndVoucherNo, VoucherStock.Status_9);
			if (stocktotal.compareTo(intototal)!=0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证号号段有误!或凭证种类有误!或凭证号状态已不是正常状态");
			}
			// 修改库存
			List<VoucherStock> stocklist = voucherStockDaoImpl
					.getVoucherStockListNoPage(webInput, StartVoucherNo,
							EndVoucherNo, VoucherStock.Status_9, detail
									.getVoucherId());
			if (stocklist == null || stocklist.size() == 0) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "凭证号号段有误库存不存在!");
			}
			for (int ii = 0; ii < stocklist.size(); ii++) {
				VoucherStock stock = stocklist.get(ii);
				// 得到入库单ID
				VoucherStock Isvoucherstock = voucherStockDaoImpl
						.getVoucherStock(stock);
				if (Isvoucherstock == null) {
					throw new OrganApplicationException(
							OrganRespCode.RC_ARGS_NULL, "", "凭证号已经不存在!");
				}
				Isvoucherstock.setStatus(VoucherStock.Status_0);
				voucherStockBusinessDaoImpl.updateVoucherStockByStatus(
						webInput, Isvoucherstock);
			}
			// 凭证出入库明细
			voucherStockDetail = new VoucherStockDetail();
			voucherStockDetail.setIoOrderId(voucherAdjustOrder1.getVoucherAdjustOrderId());
			voucherStockDetail.setBranchNo(voucherAdjustOrder1.getBranchNo());
			voucherStockDetail.setTranDate(ic.getTranDate());
			voucherStockDetail.setTranTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			voucherStockDetail.setIssueCompanyNo(voucherAdjustOrder1.getIssueCompanyNo());
			voucherStockDetail.setVoucherId(detail.getVoucherId());
			voucherStockDetail.setStartVoucherNo(detail.getStartVoucherNo());
			voucherStockDetail.setEndVoucherNo(detail.getEndVoucherNo());
			
			if (voucherAdjustOrder1.getTranType().equals(
					VoucherAdjustOrder.TranType_11)) {// 丢失取消
				voucherStockDetail.setTranType(VoucherStockDetail.Status_9);
				voucherStockDetail.setCount(-detail.getCount());	
			} else if (voucherAdjustOrder1.getTranType().equals(
					VoucherAdjustOrder.TranType_12)) {// 报损取消
				voucherStockDetail.setTranType(VoucherStockDetail.Status_9);
				voucherStockDetail.setCount(-detail.getCount());	
			} else if (voucherAdjustOrder1.getTranType().equals(
					VoucherAdjustOrder.TranType_13)) {// 盘盈取消
				voucherStockDetail.setTranType(VoucherStockDetail.Status_3);
				voucherStockDetail.setCount(-detail.getCount());	
			} 
		    
			voucherStockDetail.setOperId(webInput.getOperId());//
			voucherStockDetailBusinessDaoImpl.insertVoucherStockDetail(webInput, voucherStockDetail);   
		}
		// 修改数据库操作日志
		insertRecordingOperationLog(webInput, "凭证调整单修改", voucherAdjustOrder1,
				SysOperLog.operType_Edit, "VoucherAdjustOrderId+"
						+ voucherAdjustOrder.getVoucherAdjustOrderId());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherAdjustOrder obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
	/**
	    * 验证号段是否重复
	    */
	@SuppressWarnings("unchecked")
	private String isVoucherNo(String Voucherno,Map map){
		Iterator ite = map.entrySet().iterator();	
		while(ite.hasNext()){		
			Map.Entry<Object, Object> entry = (Entry<Object, Object>) ite.next();	
			Object key = entry.getKey();	
			Object value = entry.getValue();
			BigInteger bigkey=new BigInteger(key.toString());
			BigInteger bigvalue=new BigInteger(value.toString());
			BigInteger bigvoucherno=new BigInteger(Voucherno);
			if(bigvoucherno.intValue()>=bigkey.intValue() && bigvoucherno.intValue()<=bigvalue.intValue())
				return "error";
		}
		return "";
	}
}
